package com.testDemo.hutool;

import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.text.csv.CsvWriter;
import cn.hutool.core.util.CharsetUtil;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: zour
 * @date: 2024/2/7
 * @description:
 */
public class CsvWriterTest {
    @Test
    public void writer() {
        List<String[]> all  = new ArrayList<>();
        for (int i = 0; i < 33; i++) {
            all.add(new String[]{"AA"+i,"CC"+i});
        }
        createCsv(all);
    }

    private void createCsv(List<String[]> all) {
        int len = all.size();

        String[] row;
        CsvWriter writer = null;
        File cvs;
        boolean existsFile = false;
        try {
            for (int i = 0; i < len; i++) {
                row = all.get(i);
                cvs = new File("c://temp/test.csv");
                existsFile = cvs.exists();
                writer = CsvUtil.getWriter(cvs, CharsetUtil.CHARSET_UTF_8, true);
                if(existsFile){
                    writer.writeLine(null);
                }
                writer.writeLine(row);
                writer.flush();

            }
        } catch (IORuntimeException e) {
            throw new RuntimeException(e);
        } finally {
            if (writer != null) {
                writer.flush();
                writer.close();
            }
        }
    }


}
